package com.ducquoc.examreviewdegreea1;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Point;
import android.graphics.PointF;
import android.os.Bundle;
import android.util.FloatMath;
import android.util.Log;
import android.util.TypedValue;
import android.view.Display;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.widget.TextView;

public class RuseActivity extends Activity implements OnTouchListener {

	enum Mode {
		NONE, DRAG, ZOOM
	}

	Mode mode = Mode.NONE;
	static final int MIN_FONT_SIZE = 20;
	static final int MAX_FONT_SIZE = 50;
	PointF start = new PointF();

	Point tvPos0 = new Point();
	Point tvPos1 = new Point();
	Point tvPosSave = new Point();

	float oldDist = 1f;
	int scrWidth;
	int scrHeight;
	TextView tv;

	@SuppressWarnings("deprecation")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_ruse);

		tv = (TextView) findViewById(R.id.tvContent);
		loadContent();
		tv.setOnTouchListener(this);
		Display display = ((WindowManager) getSystemService(WINDOW_SERVICE))
				.getDefaultDisplay();
		scrWidth = display.getWidth();
		scrHeight = display.getHeight();
	}

	private void loadContent() {

		String data;
		InputStream in = getResources().openRawResource(R.drawable.ruse_content);
		InputStreamReader inreader = new InputStreamReader(in);
		BufferedReader bufreader = new BufferedReader(inreader);
		StringBuilder builder = new StringBuilder();
		if (in != null) {
			try {
				while ((data = bufreader.readLine()) != null) {
					builder.append(data);
					builder.append("\n");
				}
				in.close();
				tv.setText(builder.toString());
			} catch (IOException ex) {
				Log.e("ERROR", ex.getMessage());
			}
		}
	}

	public boolean onTouch(View v, MotionEvent event) {
		switch (event.getAction() & MotionEvent.ACTION_MASK) {
		case MotionEvent.ACTION_DOWN:
			start.set(event.getX(), event.getY());
			tvPos0.set((int) event.getX(), (int) event.getY());
			mode = Mode.DRAG;
			break;
		case MotionEvent.ACTION_POINTER_DOWN:
			oldDist = spacing(event);
			if (oldDist > 10f) {
				mode = Mode.ZOOM;
			}
			break;
		case MotionEvent.ACTION_UP:
		case MotionEvent.ACTION_POINTER_UP:
			mode = Mode.NONE;
			tvPos1.set(tvPosSave.x, tvPosSave.y);
			break;
		case MotionEvent.ACTION_MOVE:
			if (mode == Mode.DRAG) {
				doScroll(event);
			} else if (mode == Mode.ZOOM) {
				doZoom(event);
			}
			break;
		}
		return true;
	}

	private void doScroll(MotionEvent event) {
		int xe = (int) event.getX();
		int ye = (int) event.getY();
		int txtHeight = tv.getLineHeight() * (tv.getLineCount() - 2);
		int xt = tvPos1.x + tvPos0.x - xe;
		int yt = tvPos1.y + tvPos0.y - ye;
		if (xt < -tv.getRight()) {
			xt = -tv.getRight() + 20;
		} else if (xt > scrWidth) {
			xt = scrWidth - 20;
		}
		if (yt < -tv.getBottom()) {
			yt = -tv.getBottom() + tv.getLineHeight();
		} else if (yt > txtHeight) {
			yt = txtHeight;
		}
		tv.scrollTo(0, yt - 500);// thay the (xt, yt) thanh (0, yt) de co dinh be rong
		tvPosSave.set(xt, yt);
	}

	void log(Object... objs) {
		StringBuilder sb = new StringBuilder();
		for (Object obj : objs) {
			sb.append(obj);
		}
		Log.d(MainActivity.class.getSimpleName(), sb.toString());
	}

	private void doZoom(MotionEvent event) {
		float newDist = spacing(event);
		if (newDist > 10f) {
			float scale = newDist / oldDist;
			if (scale > 1) {
				scale = 1.1f;
			} else if (scale < 1) {
				scale = 0.95f;
			}
			float currSize = tv.getTextSize() * scale;
			if ((currSize < MAX_FONT_SIZE && currSize > MIN_FONT_SIZE)
					|| (currSize >= MAX_FONT_SIZE && scale < 1)
					|| (currSize <= MIN_FONT_SIZE && scale > 1)) {
				tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, currSize);
			}
		}
	}

	/** Determine the space between the first two fingers */
	@SuppressLint("FloatMath")
	private float spacing(MotionEvent event) {
		float x = event.getX(0) - event.getX(1);
		float y = event.getY(0) - event.getY(1);
		return FloatMath.sqrt(x * x + y * y);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.ruse, menu);
		return true;
	}
}